<?php

/**
 * 我的推荐
 */
namespace app\sale\api;

class UserHasApi extends \app\member\api\MemberApi {

    public function index() {

        $pageLimit = $this->data['limit'] ? $this->data['limit'] : 10;

        $type = intval($this->data['type']);
        $type  = $type ? $type : 1;

        $config = target('sale/SaleConfig')->getConfig();

        $max = $type;
        if($max > $config['sale_level']) {
            $max = 1;
        }

        $userList = target('sale/SaleUser')->levelList($this->userId, $config['sale_level']);

        $saleList = [];
        $saleNames = explode(',', $config['sale_level_name']);

        for ($i = 1; $i <= $config['sale_level']; $i++) {
            $saleList[] = [
                'type' => $i,
                'name' => $saleNames[$i - 1] ? $saleNames[$i - 1] : $i . '级会员',
                'total' => count($userList[$i])
            ];
        }


        $count = $total[$max - 1];
        $pageData = $this->pageData($count, 10, ['type' => $type]);


        if($userList[$max]) {
            $list = array_slice($userList[$max], $pageData['limit'][0], $pageData['limit'][1]);
        }else {
            $list = [];
        }

        if ($list) {
            $this->success('ok', [
                'data' => $list,
                'pageData' => [
                    'pageLimit' => $pageLimit,
                    'limit' => count($list),
                    'page' => $pageData['page'],
                    'totalPage' => $pageData['totalPage']
                ]
            ]);
        } else {
            $this->error('暂无更记录', 404);
        }

    }


}